﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
namespace BankingSystem.Dal.Repository
{
    public abstract class RepositoryBase<T>
        where T:IEntity
    {
        protected Dictionary<string, T> _local  = new Dictionary<string, T>();
        public void Insert(T entity)
        {
            if (!_local.ContainsValue(entity))
            {
                _local.Add(entity.Id, entity);
            }
        }
        public void Update(T entity)
        {
            if (_local.ContainsValue(entity))
            {
                _local[entity.Id] = entity;
            }
        }
        public void Delete(T entity)
        {
            if (_local.ContainsValue(entity))
            {
                _local.Remove(entity.Id);
            }
        }
        public IEnumerable<T> GetAll()
        {
            return _local.Values;
        }

        public T GetByIdOrDefault(string Id)
        {
            if (_local.ContainsKey(Id))
            {
                return _local[Id];
            }
            return default(T);
        }

        public IEnumerable<T>  GetByCriteria(Func<T, bool> predicate )
        {
            return _local.Values.Where(predicate);
        }
        public abstract void Save();
        public abstract void CheckOut();
    }
}
